<?php

class Document{
	//static vars
	public static 
			$SENDER_FORM_ID="___sender_form",
			$SEND_ELEMENT_NAME="___send_element",
			$FRODO_NAMESPACE="http://www.cozzy.net/frodo/";
	
	
	/**
	 * Enter description here...
	 *
	 * @var DOMDocument
	 */
	private $_document;
	public function  __construct($source){
		$doc = new DOMDocument();
		$doc->preserveWhiteSpace = false;
		//$doc->formatOutput   = true;
		$doc->loadXML($source);
		$this->_document=$doc;
	   }
	
	/**
	 * Возвращает массив всех элементов из пространства имен frodo
	 *
	 * @return DOMNode
	 */
	public function getFrodoElements(){
		return $this->_document->getElementsByTagNameNS("http://www.cozzy.net/frodo/","*");
	}
	
	public function getDOM(){
		return $this->_document;
	}
	
	/**
	 * Возвращает элемент head
	 *
	 * @return DOMNode
	 */
	public function getHead(){
		return $this->_document->getElementsByTagName('head')->item(0);
	}
	
	/**
	 * Добавляет элемент в head
	 *
	 * @param DOMNode $element
	 */
	public function addHeadElement(DOMNode $element){
		$this->getHead()->appendChild($element);
	}
	
	/**
	 * Возвращает элемент body
	 *
	 * @return DOMNode
	 */
	public function getBody(){
		return $this->_document->getElementsByTagName("body")->item(0);
	}
	
	/**
	 * Добавляет элемент в body
	 *
	 * @param DOMNode $element
	 */
	public function addBodyElement(DOMNode $element){
		$this->getBody()->appendChild($element);
	}
	
	/**
	 * Enter description here...
	 *
	 * @return array
	 */
	public function getBodyElements(){
		return $this->getBody()->getElementsByTagName("*");
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $url
	 * @return DOMNode
	 */
	public function createScript($url){
		$script = $this->_document->createElement("script");
		$script->setAttribute("src",$url);
		return $script;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $id
	 * @param string $type
	 * @param string $name
	 * @param string $value
	 * @return DOMNode
	 */
	public function createInput($id,$type,$name="",$value=""){
		$input = $this->_document->createElement("input");
		$input->setAttribute("id",$id);
		$input->setAttribute("type",$type);
		if ($name!="") {
			$input->setAttribute("name",$name);
		}
		if ($value!="") {
			$input->setAttribute("value",$value);
		}
		return $input;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $id
	 * @param string $action
	 * @param string $method
	 * @return DOMNode
	 */
	public function createForm($id,$action,$method="post"){
		$form = $this->_document->createElement("form");
		$form->setAttribute("id",$id);
		$form->setAttribute("action",$action);
		$form->setAttribute("method",$method);
		return $form;
	}
	
	
	public function createNode($name,$attributes){
		$node=$this->_document->createElement($name);
		foreach ($attributes as $aName=>$aValue){
			$node->setAttribute($aName,$aValue);
		}
		return $node;
	}
	
	public function asString(){
		return $this->_document->saveHTML();
	}
}


?>